package com.ruoyi.articleUpvote.service.impl;

import java.util.*;

import com.ruoyi.article.domain.Article;
import com.ruoyi.article.service.IArticleService;
import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.articleUpvote.mapper.ArticleUpvoteMapper;
import com.ruoyi.articleUpvote.domain.ArticleUpvote;
import com.ruoyi.articleUpvote.service.IArticleUpvoteService;

import javax.annotation.Resource;

/**
 * 文章点赞Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-08
 */
@Service
public class ArticleUpvoteServiceImpl implements IArticleUpvoteService 
{
    @Resource
    private ArticleUpvoteMapper articleUpvoteMapper;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询文章点赞
     * 
     * @param id 文章点赞主键
     * @return 文章点赞
     */
    @Override
    public ArticleUpvote selectArticleUpvoteById(Integer id)
    {
        return articleUpvoteMapper.selectArticleUpvoteById(id);
    }

    /**
     * 查询文章点赞列表
     * 
     * @param articleUpvote 文章点赞
     * @return 文章点赞
     */
    @Override
    public List<ArticleUpvote> selectArticleUpvoteList(ArticleUpvote articleUpvote)
    {
        return articleUpvoteMapper.selectArticleUpvoteList(articleUpvote);
    }

    // 查询该用户是否对该文章点赞
    public ArticleUpvote selectArticleUpvote(Integer articleId,Integer userId){
        ArticleUpvote articleUpvote = new ArticleUpvote();
        articleUpvote.setArticleId(articleId);
        articleUpvote.setUserId(userId);

        Article article = redisCache.getCacheObject("article:details:"+articleId);

           List<ArticleUpvote> list = articleUpvoteMapper.selectArticleUpvoteList(articleUpvote);

           if (list == null || list.isEmpty()){
               articleUpvote.setIsDeleted(1);
               return articleUpvote;
           }
           return list.get(0);
    }

    /**
     * 新增文章点赞
     * 
     * @param articleUpvote 文章点赞
     * @return 结果
     */
    @Override
    public int insertArticleUpvote(ArticleUpvote articleUpvote)
    {
        return articleUpvoteMapper.insertArticleUpvote(articleUpvote);
    }

    /**
     * 修改文章点赞
     * 
     * @param articleUpvote 文章点赞
     * @return 结果
     */
    @Resource
    private IArticleService articleService;
// 更改用户对文章的点赞信息
    @Override
    public Integer updateArticleUpvote(ArticleUpvote articleUpvote)
    {
        List<ArticleUpvote> articleUpvotes = articleUpvoteMapper.selectArticleUpvoteList(articleUpvote);
        if (articleUpvotes.isEmpty()){
            return articleUpvoteMapper.insertArticleUpvote(articleUpvote);
        }
        ArticleUpvote articleUpvote1 = articleUpvotes.get(0);
        if (articleUpvote.getIsDeleted() == 0){
            articleUpvote1.setIsDeleted(0);
        }else {
            articleUpvote1.setIsDeleted(1);
        }
        return articleUpvoteMapper.updateArticleUpvote(articleUpvote1);
//        Article article = redisCache.getCacheObject("article:details:"+articleUpvote.getArticleId());
//        if (article == null){
//            article = articleService.selectArticleDetails(articleUpvote.getArticleId());
//        }
//
//        List<ArticleUpvote> articleUpvoteList = article.getArticleUpvoteList();
//        if (articleUpvoteList == null || articleUpvoteList.isEmpty()){
//            articleUpvoteList.add(articleUpvote);
//            article.setArticleUpvoteList(articleUpvoteList);
//            Integer articleUpvoteCount = articleUpvoteMapper.getArticleUpvoteCount(articleUpvote.getArticleId());
//            redisCache.setCacheObject("article:details:"+articleUpvote.getArticleId(),article);
//            return 1;
//        }
//        for (ArticleUpvote upvote : articleUpvoteList) {
//            if (upvote.getArticleId() == articleUpvote.getArticleId() && upvote.getUserId() == articleUpvote.getUserId()){
//                articleUpvoteList.remove(upvote);
//                articleUpvoteList.add(articleUpvote);
//                article.setArticleUpvoteList(articleUpvoteList);
//
//                redisCache.setCacheObject("article:details:"+articleUpvote.getArticleId(),article);
//                return 1;
//            }
//        }
//
//        articleUpvoteList.add(articleUpvote);
//
//        redisCache.setCacheObject("article:details:"+articleUpvote.getArticleId(),article);
//        System.out.println("走着");
//        return 1;
    }

    /**
     * 批量删除文章点赞
     * 
     * @param ids 需要删除的文章点赞主键
     * @return 结果
     */
    @Override
    public int deleteArticleUpvoteByIds(Integer[] ids)
    {
        return articleUpvoteMapper.deleteArticleUpvoteByIds(ids);
    }

    /**
     * 删除文章点赞信息
     * 
     * @param id 文章点赞主键
     * @return 结果
     */
    @Override
    public int deleteArticleUpvoteById(Integer id)
    {
        return articleUpvoteMapper.deleteArticleUpvoteById(id);
    }

    public List<ArticleUpvote> selectArticleUpvoteAll(){
        ArticleUpvote articleUpvote = new ArticleUpvote();
        return articleUpvoteMapper.selectArticleUpvoteList(articleUpvote);
    }

    public List<ArticleUpvote> selectArticleUpvoteByArticleId(Integer articleId){
        ArticleUpvote articleUpvote = new ArticleUpvote();
        articleUpvote.setArticleId(articleId);
        return articleUpvoteMapper.selectArticleUpvoteList(articleUpvote);
    }


    public Integer getArticleUpvoteCount(Integer articleId){
        return articleUpvoteMapper.getArticleUpvoteCount(articleId);
    }

}
